package com.scientific.research.gobang.service.user.impl;

import com.scientific.research.gobang.service.SendEmailService;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.util.UUID;

@Service
public class SendEmailServiceImpl implements SendEmailService {

    @Override
    public String toSendEmail(String email) {
        String keyWord = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        keyWord = keyWord.substring(0, 16);
        keyWord = keyWord.substring(0, 4) + "-" + keyWord.substring(4, 8) + "-" + keyWord.substring(8, 12) + "-" + keyWord.substring(12, 16);
        String content = "<body>\n" +
                "\t<style>\n" +
                "\t@media screen and (min-device-aspect-ratio : 1/1) {\n" +
                "\t\t.mydiv {\n" +
                "\t\t\tleft: 35%;\n" +
                "\t\t\twidth: 30%;\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\t@media screen and (max-device-aspect-ratio : 1/1) {\n" +
                "\t\t.mydiv {\n" +
                "\t\t\tleft: 10%;\n" +
                "\t\t\twidth: 80%;\n" +
                "\t\t}\n" +
                "\t}\n" +
                "\t</style>\n" +
                "\t\t<div class=\"mydiv\" style=\"border: 1px solid rgba(0,0,0,0.2);position: relative;border-radius: 10px;padding: 1%\">\n" +
                "\t\t\n" +
                "\t\t\t<p>尊敬的用户,您好：</p>\n" +
                "\t\t\t<hr style=\"height:1px;border:none;border-top:1px solid lightgray;width: 90%\" />\n" +
                "\t\t\t<p>&nbsp;&nbsp;&nbsp;&nbsp;您的帐号 <font style=\"font-family: Consolas;font-size: 20px;color: skyblue\">" + email + "</font> 正在进行找回密码的重置操作，如不认识此帐号请忽略该邮箱。</p>\n" +
                "\t\t\t\n" +
                "\t\t\t<p>本次请求的邮件验证码如下：</p>\n" +
                "\t\t\t\n" +
                "\t\t\t<div style=\"border-radius: 7px;background-color: rgba(0,0,0,0.05)\" align=\"center\">\n" +
                "\t\t\t\t<h3>" + keyWord + "</h3>\n" +
                "\t\t\t</div>\n" +
                "\t\t\t<p>请将该16位验证码填写至找回页面完成密码找回。(为了保证您账号的安全性，请在5分钟内完成设置并不要将该验证码泄露给他人)。</p>\n" +
                "\t\t\t<p>如非本人操作，及时检查账号或联系在线客服。</p>\n" +
                "\t\t\t<p>祝您在【五子棋算法对垒系统】收获愉快！</p>\n" +
                "\t\t\t<hr style=\"height:1px;border:none;border-top:1px solid lightgray;width: 90%\" />\n" +
                "\t\t\t<p>（这是一封自动发送的邮件，请不要直接回复，如有需要请联系管理员）</p>\n" +
                "\t\t</div>\n" +
                "\t</body>\n";
        sendFileMail("【五子棋算法对垒系统】密码找回", content, email);
        return keyWord;
    }

    @Override
    public void sendFileMail(String subject, String content, String email) {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设置自己登陆email的服务商提供的host
        senderImpl.setHost("smtp.qq.com");
        // 设置端口号
        senderImpl.setPort(587);
        // 设置自己登陆邮箱账号
        senderImpl.setUsername("2674891231@qq.com");
        // 邮箱授权码
        senderImpl.setPassword("xyzvcvffenqddjec");
        try {
            // 建立HTML邮件消息
            MimeMessage mailMessage = senderImpl.createMimeMessage();
            // true表示开始附件模式.如果邮件不需要附件设置成false即可
            MimeMessageHelper messageHelper = new MimeMessageHelper(
                    mailMessage, false, "utf-8");
            // 设置收信人的email地址
            messageHelper.setTo(email);
            // 设置寄信人的email地址{与上面登陆的邮件一致}
            messageHelper.setFrom("2674891231@qq.com");
            // 设置邮件发送内容的主题
            messageHelper.setSubject(subject);
            // true 表示启动HTML格式的邮件
            messageHelper.setText("<html>"
                    + content + "</html>", true);
            senderImpl.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
